package com.app.ytong.other;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.app.ytong.R;
import com.app.ytong.basis.BasisActivity;
import com.app.ytong.basis.Constants;
import com.app.ytong.bean.BasisBean;
import com.github.barteksc.pdfviewer.PDFView;

import java.io.File;

import butterknife.BindView;
import my.FileUtils;
import my.http.MyHttpListener;
import my.http.MyHttpUtils;


/**
 * @作者 fourfaith
 * @备注 pdf查看activity发、
 */
public class PdfScanActivity extends BasisActivity  {

    @BindView(R.id.pdfView)
    PDFView pdfView;
    String assetPath;

    public static void toPdfScanActivity(Context context, String path,
                                         String title) {
        Intent intent = new Intent(context, PdfScanActivity.class);
        intent.putExtra("path", path);
        intent.putExtra("title", title);
        context.startActivity(intent);
    }


    @Override
    public void initViews() {
        // TODO Auto-generated method stub
        super.initViews();
        setContentView(R.layout.user_pdf_scan_activity);
        mContext = this;
        setTitle("pdf浏览");
        setTitleLeftButton(null);

    }

    @Override
    public void initData(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.initData(savedInstanceState);
        String title = getIntent().getStringExtra("title");

        assetPath = getIntent().getStringExtra("path");
        if (TextUtils.isEmpty(assetPath)) {
            showToast("路径不存在");
            finish();
            return;
        }

        if (!TextUtils.isEmpty(title)) {
            setTitle(title);
        }else {
            if(assetPath.contains("/")){
               String fileName =  FileUtils.getFileNameFromUrlSimple(assetPath);
               setTitle(fileName);
            }

        }

        if(!TextUtils.isEmpty(assetPath) && (assetPath.startsWith("http:") || assetPath.startsWith("https:"))){
            showLoadingDialog("正在下载");
            MyHttpUtils.download(assetPath, Constants.DIR_TEMP, FileUtils.getFileNameFromUrlSimple(assetPath), new MyHttpListener() {
                @Override
                public void onSuccess(int httpWhat, Object result) {
                    if(isFinishing())return;
                    assetPath = ((BasisBean)result).getResultData();
                    setView();
                }

                @Override
                public void onFinish(int httpWhat) {
                    if(isFinishing())return;
                    hideLoading();
                }
            });
        }else {
            setView();
        }
    }

    void setView() {
        if(assetPath.startsWith("/storage/")){
            pdfView.fromFile(new File(assetPath))
                    .enableSwipe(true) // allows to block changing pages using swipe
                    .swipeHorizontal(false)
                    .enableDoubletap(true)
                    .defaultPage(0)
//				.onTap(onTapListener)
                    .enableAnnotationRendering(false) // render annotations (such as comments, colors or forms)
                    .password(null)
                    .scrollHandle(null)
                    .enableAntialiasing(true) // improve rendering a little bit on low-res screens
                    .load();
        }else {
            pdfView.fromAsset(assetPath)
                    .enableSwipe(true) // allows to block changing pages using swipe
                    .swipeHorizontal(false)
                    .enableDoubletap(true)
                    .defaultPage(0)
//				.onTap(onTapListener)
                    .enableAnnotationRendering(false) // render annotations (such as comments, colors or forms)
                    .password(null)
                    .scrollHandle(null)
                    .enableAntialiasing(true) // improve rendering a little bit on low-res screens
                    .load();
        }

    }

    void downloadFile(String url){

    }

}
